<template>
  <div class="main">
    <div class="tags">
      <span class="title">
        兴趣标签
      </span>
      <el-divider></el-divider>
      <div v-loading="loading">
        <div v-if="tags.length > 0">
          <el-tag :key="tag" v-for="tag in tags">{{ tag }}</el-tag>
        </div>
        <div v-else><span>待添加</span></div>
      </div>
    </div>
    <div class="learning-recently">
      <span class="title">
        最近在学
      </span>
      <el-divider></el-divider>
      <div v-loading="loading">
        <el-empty v-if="routes.length === 0" description="好像什么也没学呢"></el-empty>
        <div v-else class="route-list">
          <div class="route" v-for="route in routes.slice(0, 3)" :key="route.route.id" @click="goRoute(route.route.id)">
            <div class="route-cover">
              <img :src="route.route.cover" alt="图片找不到了QAQ">
            </div>
            <div class="route-title">
              {{ route.route.title }}
            </div>
            <div class="route-progress">
              <div class="route-progress-title">
                学习进度
              </div>
              <el-progress type="line" :percentage="Math.floor((route.progress / route.route.nodes.length) * 100)">
              </el-progress>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="follow">
      <span class="title">
        关注
      </span>
      <el-divider></el-divider>
      <div v-loading="loading">
        <el-empty v-if="follows.length === 0" description="好像谁也没关注呢"></el-empty>
        <div v-else class="follow-list">
          <div class="follow-item" v-for="follow in follows" :key="follow.id" @click="goUser(follow.id)">
            <div class="follow-item-avatar" style="display: inline-block;">
              <el-avatar :src="follow.avatar"></el-avatar>
            </div>
            <div class="follow-item-name" style="display: inline-block;">
              {{ follow.username }}
            </div>
          </div>
          <el-pagination :hide-on-single-page="false" :total="follows.length" layout="prev, pager, next">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import request from '@/API/request';
import { Message } from 'element-ui';
export default {
  name: 'Overview',
  mounted() {
    request.get('/user/get/userinfo', {
      params: {
        id: this.$route.query.id,
      },
    }).then((res) => {
      if (res.data.code === 200) {
        this.tags = res.data.data.tags;
      } else {
        Message({
          message: res.data.msg,
          type: 'error',
          customClass: 'message-global',
        });
      }
    }).catch((err) => {
      Message({
        message: err,
        type: 'error',
        customClass: 'message-global',
      });
    });
    request.get('/route/user/learn', {
      params: {
        id: this.$route.query.id,
      },
    }).then((res) => {
      if (res.data.code === 200) {
        this.routes = res.data.data;
      } else {
        Message({
          message: res.data.msg,
          type: 'error',
          customClass: 'message-global',
        });
      }
    }).catch((err) => {
      Message({
        message: err,
        type: 'error',
        customClass: 'message-global',
      });
    });
    request.get('/user/get/subscribe', {
      params: {
        id: this.$route.query.id,
      },
    }).then((res) => {
      if (res.data.code === 200) {
        this.follows = res.data.data;
      } else {
        Message({
          message: res.data.msg,
          type: 'error',
          customClass: 'message-global',
        });
      }
    }).catch((err) => {
      Message({
        message: err,
        type: 'error',
        customClass: 'message-global',
      });
    });
    this.loading = false;
  },
  data() {
    return {
      tags: [],
      routes: [],
      follows: [],
      loading: true,
    }
  },
  methods: {
    goUser(id) {
      this.$router.push({
        path: '/user',
        query: {
          id: id,
        },
      })
    },
    goRoute(id) {
      this.$router.push({
        path: '/route',
        query: {
          id: id,
        },
      })
    },
  },
  beforeRouteUpdate(to, from, next) {
    this.loading = true;
    request.get('/user/get/userinfo', {
      params: {
        id: to.query.id,
      },
    }).then((res) => {
      if (res.data.code === 200) {
        this.tags = res.data.data.tags;
      } else {
        Message({
          message: res.data.msg,
          type: 'error',
          customClass: 'message-global',
        });
      }
    }).catch((err) => {
      Message({
        message: err,
        type: 'error',
        customClass: 'message-global',
      });
    });
    request.get('/route/user/learn', {
      params: {
        id: to.query.id,
      },
    }).then((res) => {
      if (res.data.code === 200) {
        this.routes = res.data.data;
      } else {
        Message({
          message: res.data.msg,
          type: 'error',
          customClass: 'message-global',
        });
      }
    }).catch((err) => {
      Message({
        message: err,
        type: 'error',
        customClass: 'message-global',
      });
    });
    request.get('/user/get/subscribe', {
      params: {
        id: to.query.id,
      },
    }).then((res) => {
      if (res.data.code === 200) {
        this.follows = res.data.data;
      } else {
        Message({
          message: res.data.msg,
          type: 'error',
          customClass: 'message-global',
        });
      }
    }).catch((err) => {
      Message({
        message: err,
        type: 'error',
        customClass: 'message-global',
      });
    });
    this.loading = false;
    next();
  },
}
</script>

<style scoped>
.main {
  min-width: 800px;
  width: 100%;
  min-height: 520px;
  background-color: #fff;
  border-radius: 5px;
}

.tags {
  border: dashed 1px rgba(207, 205, 205, 0.842);
  margin: 5px;
  background-color: #fff;
  text-align: left;
  padding: 10px;
  border-radius: 4px;
}

.learning-recently {
  border: dashed 1px rgba(207, 205, 205, 0.842);
  margin: 5px;
  background-color: #fff;
  text-align: left;
  padding: 10px;
  border-radius: 4px;
}

.follow {
  border: dashed 1px rgba(207, 205, 205, 0.842);
  margin: 5px;
  background-color: #fff;
  text-align: left;
  padding: 10px;
  border-radius: 4px;
}

.title {
  display: block;
  font-size: 18px;
  line-height: 20px;
  font-weight: 500;
}

.el-tag {
  margin-top: 10px;
  margin-right: 10px;
}

.route-list {
  min-height: 250px;
}

.follow-list {
  min-height: 250px;
}

.route {
  display: inline-block;
  width: 300px;
  border: solid 1px rgba(207, 205, 205, 0.842);
  border-radius: 4px;
  margin-right: 20px;
  margin-top: 10px;
  transition: all 0.5s ease;
}

img {
  width: 100%;
  height: 180px;
  border-radius: 4px 4px 0 0;
  object-fit: cover;
}

.route:hover {
  border: solid 1px #409EFF;
  box-shadow: 0 0 10px #409EFF;
  cursor: pointer;
}

.route-title {
  margin-left: 10px;
  margin-top: 2px;
  margin-right: 10px;
  font-size: 20px;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.route-progress {
  margin-left: 10px;
  margin-top: 2px;
  margin-right: 10px;
}

.route-progress-title {
  vertical-align: middle;
  font-size: 15px;
  position: relative;
  left: 0px;
  top: 0px;
}

.follow-item {
  position: relative;
  display: block;
  border: solid 1px rgba(207, 205, 205, 0.842);
  margin-bottom: 3px;
  border-radius: 4px;
  transition: all 0.5s ease;
}

.follow-item:hover {
  background-color: #eff2f8;
  cursor: pointer;
}

.follow-item-avatar {
  position: relative;
  top: 4px;
  left: 10px;
}

.follow-item-name {
  vertical-align: middle;
  position: relative;
  top: -10px;
  left: 50px;
  font-size: 15px;
}

.el-pagination {
  text-align: center;
}

.el-divider {
  margin-top: 10px;
  margin-bottom: 10px;
}
</style>